package com.teamone.etl.interceptor;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.api.R;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @Description 拦截器配置
 * @Author WangZhuoRan
 * @Email  2908540863@qq.com
 * @Create 2021-01-22 15:10
 * @Program star-etl
 */
public class WebSpringRedisSessionInterceptor implements HandlerInterceptor {

    private static final String USER_ID = "user-ID";
    private static final String ADMIN_ID = "admin-ID";
    private static final int ADMIN = 80;
    private static final int USER = 4396;
    @Resource
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public boolean preHandle (HttpServletRequest request, HttpServletResponse response, Object handler) {

        HttpSession session = request.getSession();
        String url = request.getRequestURL().toString();
        String ip = request.getServerName();
        int port = request.getServerPort();
        System.out.println(url);
        System.out.println(ip);
        System.out.println(port);
        System.out.println("session--"+session.getId());
        if (session.getAttribute(USER_ID) != null || session.getAttribute(ADMIN_ID) != null) {
            try {
                if (port == USER) {
                    int userId = Integer.parseInt(session.getAttribute(USER_ID).toString());
                    String sessionId = redisTemplate.opsForValue().get(USER_ID + userId);
                    System.out.println("redis--sessionid--"+sessionId);
                    if (sessionId != null && sessionId.equals(session.getId())) {
                        return true;
                    }
                } else if (port == ADMIN) {
                    int adminId = Integer.parseInt(session.getAttribute(ADMIN_ID).toString());
                    String sessionId = redisTemplate.opsForValue().get(ADMIN_ID + adminId);
                    System.out.println("redis--sessionid--"+sessionId);
                    if (sessionId != null && sessionId.equals(session.getId())) {
                        return true;
                    }
                } else {
//                    response404(response);
                    System.out.println("interceptor>>>>>>放行"+port);
                    return true;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        response401(response);
        return false;
    }

    private void response404 (HttpServletResponse response) {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        try {
            response.getWriter().print(JSON.toJSONString(R.failed("Page Not Found 404")));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void response401 (HttpServletResponse response) {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        try {
            response.getWriter().print(JSON.toJSONString(R.failed("请先登录")));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}